package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    @Autowired
    private WmChannelMapper wmChannelMapper;
    @Autowired
    private WmNewsMapper wmNewsMapper;
    @Override
    public ResponseResult channelList() {
        //分页  限制接口查询数据最大值
        Page<WmChannel> page = new Page<>(1,100);
        //列表查询
        Page<WmChannel> wmChannelPage = wmChannelMapper.selectPage(page, null);
        //返回数据
        return ResponseResult.okResult(wmChannelPage.getRecords());
    }


    //新增频道
    @Override
    public ResponseResult inster(WmChannel wmChannel) {
        if (wmChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道不能为空");
        }
        //查重查询是否存在selectCount
        LambdaQueryWrapper<WmChannel> wrapper=new LambdaQueryWrapper();
        wrapper.eq(WmChannel::getName,wmChannel.getName());
        WmChannel wmChannel1 = getOne(wrapper);
        if (wmChannel1!=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道存在");
        }

        wmChannel.setCreatedTime(new Date());
        save(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    //查询列表
    @Override
    public ResponseResult find(ChannelDto dto) {
        //入参判断
        if (dto==null){
            return new PageResponseResult(0,0,0);
        }

        Integer page = dto.getPage();
        Integer size = dto.getSize();

        page=page==null?1:page;
        size=size==null?10:size;
        Page<WmChannel> pageInfo = new Page<>(page,size);
        //组装条件查询
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
        String name = dto.getName();
        wrapper.like(StringUtils.isNotBlank(name),WmChannel::getName,name);
        wrapper.orderByDesc(WmChannel::getCreatedTime);
        Page<WmChannel> wmChannelPage = wmChannelMapper.selectPage(pageInfo, wrapper);

        long total = wmChannelPage.getTotal();
        PageResponseResult result = new PageResponseResult(page,size, (int) total);
        result.setData(wmChannelPage.getRecords());
        return result;
    }

    @Override
    public ResponseResult update(WmChannel wmChannel) {
        if (wmChannel==null&&wmChannel.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

//        String name = wmChannel.getName();
//        LambdaQueryWrapper<WmChannel> wmChannelWrapper = new LambdaQueryWrapper<>();
//        wmChannelWrapper.eq(WmChannel::getName,name);
//        Integer count = wmChannelMapper.selectCount(wmChannelWrapper);
//        if (count>0){
//            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"重名");
//        }

        LambdaQueryWrapper<WmNews> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmNews::getChannelId,wmChannel.getId());
        wrapper.eq(WmNews::getStatus,9);
        Integer count1 = wmNewsMapper.selectCount(wrapper);
        if (count1>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"包含新闻");
        }
        updateById(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    //删除
    @Override
    public ResponseResult delete(Integer id) {
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        WmChannel wmChannel = getById(id);
        if (wmChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        Boolean status = wmChannel.getStatus();
        if (status){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"Bunengshanchu");
        }

        LambdaQueryWrapper<WmNews> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmNews::getChannelId,wmChannel.getId());
        wrapper.eq(WmNews::getStatus,9);
        Integer count1 = wmNewsMapper.selectCount(wrapper);
        if (count1>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"包含新闻");
        }

        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }
}